import storage from 'good-storage'
const user_info_prefix = 'vue_mooc_user'
const user_address_prefix = 'vue_mooc_user_address'

// 用户模块
export function setUserInfo (userinfo) {
  storage.set(user_info_prefix, userinfo)
}
export function getUserInfo () {
  return storage.get(user_info_prefix, {})
}
export function setUserAddress (address) {
  storage.set(user_address_prefix, address)
}
export function getUserAddress () {
  return storage.get(user_address_prefix, [])
}

const TokenKey = 'USER-TOKEN'

export function getToken () {
  return storage.get(TokenKey)
}

export function setToken (token) {
  return storage.set(TokenKey,token)
}

export function removeToken () {
  return storage.remove(TokenKey)
}


import { getInfo } from '@/api/user';
import store from '@/store/index'
import { Message } from 'element-ui'


// 验证是否登录，验证vuex中是否有token，并且向后台发送验证请求
export async function isLogin () {

  // 无token
  if (!(store.getters.id && store.getters.token)){
    Message({
      type: 'error',
      message: '请先登录系统！'
    })
    return false
  }
  // 有token，向服务器发送验证请求
  getInfo().then(result => {
    if (result.code === 20000){
      return true
    }
    else {
      return false
    }
  })
}
